package servlet;
import bean.Login;
import bean.Users;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.ArrayList;

@WebServlet(name = "RegisterServlet", urlPatterns = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    String loginName;
    String password;
    String again_password;
    String phoneNumber;
    Statement sta;ResultSet rs;Users userBean= new Users();String userID;PreparedStatement stat;Connection conn;
    public void init(ServletConfig config)throws ServletException{
        super.init(config);
    }
    public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        request.setAttribute("userBean",userBean);
        loginName=request.getParameter("loginName").trim();
        password=request.getParameter("password").trim();
        again_password=request.getParameter("again_password").trim();
        phoneNumber=request.getParameter("phoneNumber").trim();
        if (!password.equals(again_password)){
            userBean.setBackNews("两次密码不同，注册失败");
            RequestDispatcher dispatcher=
                    request.getRequestDispatcher("register.jsp");
            dispatcher.forward(request,response);
            return;
        }
        boolean isLD=true;
        for (int i=0;i<loginName.length();i++){
            char c=loginName.charAt(i);
            if(!(Character.isLetterOrDigit(c)||c=='_')) isLD=false;
        }
        boolean boo=loginName.length()>0&&password.length()>0&&isLD;
        String backNews="";
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system", "root", "123456");
            String users="Select UserID from users";
            sta=conn.createStatement();
            rs=sta.executeQuery(users);
            rs.beforeFirst();
            ArrayList<String>check=new ArrayList<>();
            while (rs.next()){
                String user=rs.getString("UserID");
                check.add(user);
            }
            conn.close();
                for (String c : check) {
                    if (c == loginName) {     //  遍历判断，若有重复值，则flag=false，并打破循环
                        userBean.setBackNews("用户名已存在");
                        RequestDispatcher dispatcher=
                                request.getRequestDispatcher("register.jsp");
                        dispatcher.forward(request,response);
                        return;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(password+phoneNumber+loginName);
        try {
                Class.forName("org.mariadb.jdbc.Driver");
                conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system", "root", "123456");
                String sql= " INSERT INTO users(Password,Tel,UserID) VALUES (?,?,?)";
                stat = conn.prepareStatement(sql);
                if (boo) {
                    password=Md5Utils.encoder (password);
                    stat.setString(1, password);
                    stat.setString(2,phoneNumber);
                    stat.setString(3,loginName);
                    stat.executeUpdate();
                    int m = stat.executeUpdate();
                    if (m != 0) {
                       backNews = "注册成功";
                       userBean.setBackNews(backNews);
                       userBean.setUserId(loginName);
                       userBean.setPassword(password);
                       userBean.setTel(phoneNumber);

                }else{
                    backNews="信息填写不完整或名字中有非法字符";
                    userBean.setBackNews(backNews);}
                    conn.close();}
                }  catch (SQLException | ClassNotFoundException | NoSuchAlgorithmException exp) {
              backNews="该用户名已被占用，请您更换名字"+exp;
              userBean.setBackNews(backNews);
        }
      RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
        dispatcher.forward(request,response);
        }
}


